﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Anata;
using Anata.AnataServerDataSetTableAdapters;

namespace Anata.TableForm
{
    public partial class AsetForm : Form
    {
        AdminDialogueForm _f;
        asetTableAdapter aset = new asetTableAdapter();
        AnataServerDataSet.asetDataTable dt = new AnataServerDataSet.asetDataTable();

        Log Log = new Log();
        _log l;
        List<_log> list = new List<_log>();

        DataRowCollection dr;
        DataRow drow;

        bool isUpdate = false;
        int id;

        public AsetForm() { }

        public AsetForm(AdminDialogueForm f)
        {
            InitializeComponent();
            _f = f;

            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "yyyy-MM-dd";
            dateTimePicker2.Format = DateTimePickerFormat.Custom;
            dateTimePicker2.CustomFormat = "yyyy-MM-dd";

            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_FormClosing);
        }

        public AsetForm(AdminDialogueForm f, DataGridView dataGridView1)
        {
            InitializeComponent();

            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "yyyy-MM-dd";
            dateTimePicker2.Format = DateTimePickerFormat.Custom;
            dateTimePicker2.CustomFormat = "yyyy-MM-dd";

            _f = f;
            isUpdate = true;
            id = Convert.ToInt32(dataGridView1[0, dataGridView1.SelectedCells[0].RowIndex].Value);

            dt = aset.GetDataByID(id);
            dr = dt.Rows;
            drow = dr[0];

            comboBox1.SelectedValue = drow[dt.id_cabangColumn].ToString();
            comboBox2.SelectedValue = drow[dt.jenis_asetColumn].ToString();
            textBox3.Text = drow[dt.namaColumn].ToString();
            textBox5.Text = drow[dt.harga_beliColumn].ToString();
            textBox1.Text = drow[dt.harga_jualColumn].ToString();
            textBox2.Text = drow[dt.mortisasiColumn].ToString();
            textBox4.Text = drow[dt.jumlahColumn].ToString();
            dateTimePicker1.Value = DateTime.Parse(drow[dt.tgl_beliColumn].ToString());
            dateTimePicker2.Value = DateTime.Parse(drow[dt.tgl_jualColumn].ToString());

            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_FormClosing);
        }

        private void Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            _f.PerformRefresh();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void insertData_Click(object sender, EventArgs e)
        {

            if (isDataValid())
            {
                if (isUpdate)
                {
                    if (comboBox1.DroppedDown)
                    {
                        l = new _log("aset", id, "id_cabang", "edit", drow[dt.id_cabangColumn].ToString(), comboBox1.SelectedValue.ToString());
                        list.Add(l);
                    }
                    if (comboBox2.DroppedDown)
                    {
                        l = new _log("aset", id, "jenis_aset", "edit", drow[dt.jenis_asetColumn].ToString(), comboBox2.SelectedValue.ToString());
                        list.Add(l);
                    }
                    if (textBox3.Modified)
                    {
                        l = new _log("aset", id, "nama", "edit", drow[dt.namaColumn].ToString(), textBox3.Text);
                        list.Add(l);
                    }
                    if (textBox5.Modified)
                    {
                        l = new _log("aset", id, "harga_beli", "edit", drow[dt.harga_beliColumn].ToString(), textBox5.Text);
                        list.Add(l);
                    }
                    if (textBox1.Modified)
                    {
                        l = new _log("aset", id, "harga_jual", "edit", drow[dt.harga_jualColumn].ToString(), textBox1.Text);
                        list.Add(l);
                    }
                    if (textBox2.Modified)
                    {
                        l = new _log("aset", id, "mortisasi", "edit", drow[dt.mortisasiColumn].ToString(), textBox2.Text);
                        list.Add(l);
                    }
                    if (textBox4.Modified)
                    {
                        l = new _log("aset", id, "jumlah", "edit", drow[dt.jumlahColumn].ToString(), textBox4.Text);
                        list.Add(l);
                    }
                    if (!dateTimePicker1.Text.Equals(drow[dt.tgl_beliColumn].ToString()))
                    {
                        l = new _log("aset", id, "tgl_beli", "edit", drow[dt.tgl_beliColumn].ToString(), dateTimePicker1.Text);
                        list.Add(l);
                    }
                    if (!dateTimePicker2.Text.Equals(drow[dt.tgl_beliColumn].ToString()))
                    {
                        l = new _log("aset", id, "tgl_jual", "edit", drow[dt.tgl_jualColumn].ToString(), dateTimePicker2.Text);
                        list.Add(l);
                    }
                    try
                    {
                        aset.UpdateQuery(Convert.ToInt32(comboBox1.SelectedValue),
                            Convert.ToInt32(comboBox2.SelectedValue),
                            textBox3.Text,
                            Convert.ToInt32(textBox5.Text),
                            Convert.ToInt32(textBox1.Text),
                            Convert.ToInt32(textBox2.Text),
                            Convert.ToInt32(textBox4.Text),
                            DateTime.Parse(dateTimePicker1.Text),
                            DateTime.Parse(dateTimePicker2.Text),
                            id);
                    }
                    catch (Exception msg) { MessageBox.Show("Update tidak berhasil, periksa kembali input yang anda masukkan."); }
                    this.Close();
                }
                else
                {
                    try
                    {
                        aset.Insert(Convert.ToInt32(comboBox1.SelectedValue),
                            Convert.ToInt32(comboBox2.SelectedValue),
                            textBox3.Text,
                            Convert.ToInt32(textBox5.Text),
                            Convert.ToInt32(textBox1.Text),
                            Convert.ToInt32(textBox2.Text),
                            Convert.ToInt32(textBox4.Text),
                            DateTime.Parse(dateTimePicker1.Text),
                            DateTime.Parse(dateTimePicker2.Text));
                    }
                    catch (Exception msg) { MessageBox.Show("Terdapat kesalahan format masukan, periksa kembali input yang anda masukkan."); }
                    this.Close();
                }
                Log.ProcessLog(list);
            }
            else { MessageBox.Show("Data yang diperlukan belum lengkap."); }
        }

        public bool isDataValid()
        {
            if (!textBox1.Text.Equals("") &&
                !textBox2.Text.Equals("") &&
                !textBox3.Text.Equals("") &&
                !textBox4.Text.Equals(""))
            { return true; }
            else { return false; }
        }

        private void AsetForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'anataServerDatabase.jenis_aset' table. You can move, or remove it, as needed.
            this.jenis_asetTableAdapter.Fill(this.anataServerDatabase.jenis_aset);
            // TODO: This line of code loads data into the 'anataServerDatabase.cabang' table. You can move, or remove it, as needed.
            this.cabangTableAdapter.Fill(this.anataServerDatabase.cabang);

        }
    }
}

